Row

Overall Sightings Map and Heat Map

Time series plot

---
title: "Interactive Data"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source: embed
    theme: simplex
runtime: shiny

---

```{r split = FALSE, fig.align = 'default', warning = FALSE, out.width="100%", include = FALSE, message= FALSE}
library(tidyverse)
library(lubridate)
library(readr) 
library("ggplot2") 
library("dplyr")
library(xts)
library("lubridate")
library("RColorBrewer")
library("ggthemes")
library("gridExtra")
library("leaflet")
library("highcharter")
library(scales)
library(leaflet.extras)

rats_raw <- read.csv("./Rat_Sightings.csv", na = c("", "NA", "N/A", "Unspecified")) %>%
  janitor::clean_names() %>% 
  mutate(created_date = mdy_hms(created_date)) %>%
  mutate(sighting_year = year(created_date),
         sighting_month_num = month(created_date),
         sighting_month = month(created_date, label = TRUE, abbr = FALSE),
         sighting_day = day(created_date),
         sighting_weekday = wday(created_date, label = TRUE, abbr = FALSE)) 
```

Column {.sidebar}
-----------------------------------------------------------------------

Welcome to our interactive dashboard. 


Row {data-width=700}
-----------------------------------------------------------------------

### Overall Sightings Map and Heat Map

```{r}
top = 40.917577 # north lat
left = -74.259090 # west long
right = -73.700272 # east long
bottom =  40.477399 # south lat


nyc = rats_raw %>%
  filter(latitude >= bottom) %>%
  filter ( latitude <= top) %>%
  filter( longitude >= left ) %>%
  filter(longitude <= right)

center_lon = median(nyc$longitude,na.rm = TRUE)
center_lat = median(nyc$latitude,na.rm = TRUE)

count = nyc %>%
  group_by(location) %>%
  count()

nyc = merge(nyc, count, by = "location")

factpal = colorFactor("blue", nyc$n)

nyc %>%
leaflet() %>% 
  addProviderTiles("Esri.NatGeoWorldMap") %>%
  addCircles(lng = ~longitude, lat = ~latitude)  %>%
  setView(lng=center_lon, lat=center_lat,zoom = 10) 

nyc %>%
  leaflet() %>%
  addProviderTiles("Esri.NatGeoWorldMap") %>%
  addHeatmap(lng = ~longitude, lat = ~latitude, intensity = ~(nyc$n), blur = 20, max = 0.05, radius = 15) %>%
  setView(lng=center_lon, lat=center_lat,zoom = 10)
```

### Time series plot 

```{r}
overall <- rats_raw %>% 
  group_by(sighting_year, sighting_month_num, sighting_day) %>% 
  summarize(count = n()) %>% 
  mutate(date = as.Date(paste(sighting_year, sighting_month_num, sighting_day, sep = "-")))

time_series = xts(overall$count , order.by= overall$date)

hchart(time_series, name = "Rat Sightings") %>% 
  hc_add_theme(hc_theme_darkunica()) %>%
  hc_credits(enabled = TRUE, text = "Sources: City of New York", style = list(fontSize = "12px")) %>%
  hc_title(text = "Time Series of NYC Rat Sightings") %>%
  hc_legend(enabled = TRUE)
```